﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace childcare
{
    public partial class frm_xeplop : Form
    {
        SqlConnection con = new SqlConnection();
        private DataTable dt = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        public frm_xeplop()
        {
            InitializeComponent();
        }

        private void frm_xeplop_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'childcareDataSet4._class' table. You can move, or remove it, as needed.
            this.classTableAdapter.Fill(this.childcareDataSet4._class);
            // TODO: This line of code loads data into the 'childcareDataSet3.teacher' table. You can move, or remove it, as needed.
            this.teacherTableAdapter.Fill(this.childcareDataSet3.teacher);
            string connect = @"Data Source=BLACK-PC\SQLEXPRESS;Initial Catalog=Childcare;Integrated Security=True";
            con = new SqlConnection(connect);
            connectdatabase.Openconnect();
            dgv_xeplop.DataSource = connectdatabase.ex("select * from xeplop");
            txtSTT.Enabled = false;
            databinding();

        }

        private void databinding()
        {
            txtSTT.DataBindings.Clear();
            txtSTT.DataBindings.Add("Text", dgv_xeplop.DataSource,"STT");
            cbo_magv.DataBindings.Clear();
            cbo_magv.DataBindings.Add("Text", dgv_xeplop.DataSource, "T_ID");
            cbo_tengv.DataBindings.Clear();
            cbo_tengv.DataBindings.Add("Text", dgv_xeplop.DataSource, "name");
            cbo_lopday.DataBindings.Clear();
            cbo_lopday.DataBindings.Add("Text", dgv_xeplop.DataSource, "CL_ID");
        }
        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
            frmadmin _frmxeplop = new frmadmin();
            _frmxeplop.Show();

        }
        private bool test()
        {
            kt = true;
            if (cbo_magv.Text == "")
            {
                MessageBox.Show("Bạn phải nhập mã giáo viên vào", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
                cbo_magv.Focus();
            }
            if (cbo_tengv.Text == "")
            {
                MessageBox.Show("Bạn phải nhập tên giáo viên vào", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
                cbo_tengv.Focus();
            }
            if (cbo_lopday.Text == "")
            {
                MessageBox.Show("Bạn phải nhập lớp vào", "THÔNG BÁO", MessageBoxButtons.OK);
                kt = false;
                cbo_lopday.Focus();
            }
            return kt;
        }
        private void btnthem_Click(object sender, EventArgs e)
        {
            test();
            if (kt == true)
            {
                    SqlCommand commandinsert = new SqlCommand();
                    commandinsert.Connection = con;
                    commandinsert.CommandType = CommandType.Text;
                    commandinsert.CommandText = "insert xeplop (T_ID,name,Cl_ID) values(@T_ID, @name, @CL_ID)";
                    SqlParameter pram = commandinsert.Parameters.Add("@T_ID", SqlDbType.VarChar, 6);
                    pram.Value = cbo_magv.Text;
                    pram = commandinsert.Parameters.Add("@name", SqlDbType.NVarChar, 25);
                    pram.Value = cbo_tengv.Text;
                    pram = commandinsert.Parameters.Add("@CL_ID", SqlDbType.VarChar, 6);
                    pram.Value = cbo_lopday.Text;
                    con.Open();
                    commandinsert.ExecuteNonQuery();
                    con.Close();
                    dgv_xeplop.DataSource = connectdatabase.ex("select * from xeplop");
                    MessageBox.Show("Bạn đã thêm thành công!", "Thông Báo", MessageBoxButtons.OK);
                    databinding();
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            test();
            if (kt == true)
            {
                SqlCommand commandupdate = new SqlCommand();
                commandupdate.Connection = con;
                commandupdate.CommandType = CommandType.Text;
                commandupdate.CommandText = @"update xeplop set T_ID = @T_ID,name = @name, CL_ID = @CL_ID  where STT = @STT";
                SqlParameter pram = commandupdate.Parameters.Add("@STT", SqlDbType.Int);
                pram.Value = txtSTT.Text;
                pram = commandupdate.Parameters.Add("@T_ID", SqlDbType.VarChar, 6);
                pram.Value = cbo_magv.Text;
                pram = commandupdate.Parameters.Add("@name", SqlDbType.NVarChar, 25);
                pram.Value = cbo_tengv.Text;
                pram = commandupdate.Parameters.Add("@CL_ID", SqlDbType.VarChar, 6);
                pram.Value = cbo_lopday.Text;
                con.Open();
                commandupdate.ExecuteNonQuery();
                con.Close();
                dgv_xeplop.DataSource = connectdatabase.ex("select * from xeplop");
                MessageBox.Show("Bạn đã Sửa thành công!", "Thông Báo", MessageBoxButtons.OK);
                databinding();
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thực sự muốn xóa không ?", "XÓA", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                SqlCommand commandelete = new SqlCommand();
                commandelete.Connection = con;
                commandelete.CommandType = CommandType.Text;
                commandelete.CommandText = @"delete from xeplop where STT = @STT";
                SqlParameter pram = commandelete.Parameters.Add("@STT", SqlDbType.VarChar, 6);
                pram.Value = txtSTT.Text;
                con.Open();
                commandelete.ExecuteNonQuery();
                con.Close();
                dgv_xeplop.DataSource = connectdatabase.ex("select * from xeplop");
                MessageBox.Show("Bạn đã Xóa thành công!", "Thông Báo", MessageBoxButtons.OK);
                databinding();
            }
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            txtSTT.Clear();
            cbo_magv.DataBindings.Clear();
        }

        private void cbo_magv_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cbo_tengv_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cbo_lopday_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btntk_Click(object sender, EventArgs e)
        {
            dt.Clear();
            SqlCommand commandsearch = new SqlCommand();
            commandsearch.Connection = con;
            commandsearch.CommandType = CommandType.Text;
            commandsearch.CommandText = "select * from xeplop where name=@name or T_ID = @T_ID";
            SqlParameter pram = commandsearch.Parameters.Add("@name", SqlDbType.NVarChar, 25);
            pram.Value = txttk.Text;
            pram = commandsearch.Parameters.Add("@T_ID", SqlDbType.VarChar, 6);
            pram.Value = txttk.Text;
            da.SelectCommand = commandsearch;
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                dgv_xeplop.DataSource = dt;
            }
            else
            {
                dgv_xeplop.DataSource = dt;
                MessageBox.Show("Không tìm thấy giáo viên này!", "THÔNG BÁO", MessageBoxButtons.OK);
            }
            databinding();
        }
    }
}
